﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Text.RegularExpressions;

namespace pnh
{
    class UpdateAchievementsPictures
    {
        public delegate void consoleWriteLineDelegate(string str);

        private consoleWriteLineDelegate delConsole;
        private pnhBrowserControl.pnhBowserControl browser;

        public UpdateAchievementsPictures(consoleWriteLineDelegate del, pnhBrowserControl.pnhBowserControl b)
        {
            delConsole = del;
            browser = b;
        }

        private enum eGameField
        {
            eGameId = 0,
            eGameMsid,
            eGameName,
            eGamertag
        };

        private void consoleWriteLine(string str)
        {
            delConsole.Invoke(str);
        }

        /// <summary>
        /// Apply all the games starting with a specific game id
        /// </summary>
        /// <param name="startGameID"></param>
        public void apply(int startGameID)
        {
            //get the list of games
            string strHtmlGamesList = DBInterface.getGamesListPerPlayer(startGameID);
            string[] strGamesArray = strHtmlGamesList.Split(',');

            //go through the list of games
            foreach (string strGame in strGamesArray)
            {
                //get the game info
                string[] strGameField = strGame.Split(';');
                consoleWriteLine(strGameField[(int)eGameField.eGameId] + " - " + strGameField[(int)eGameField.eGameName] + " - " + strGameField[(int)eGameField.eGamertag]);
                

                //get the achievements page
                browser.getWebPageAchievement("about:blank");
                string url = "https://live.xbox.com/fr-FR/Activity/Details?titleId=" + strGameField[(int)eGameField.eGameMsid] + "&compareTo=" + strGameField[(int)eGameField.eGamertag];
                string html = browser.getWebPageAchievement(url);

                //extract achievements data
                JSONData achievements = Extract.extractAchievements(html);
                if (achievements == null)
                    continue;

                //create directory
                DBInterface.ftpCreateDirectory(Constant.FTP_ACHE_IMG + "/" + strGameField[(int)eGameField.eGameId]);

                //go through each achievements
                foreach (JSONAchievement ache in achievements._achievements)
                {
                    //upload the picture to the ftp
                    DBInterface.getImg(ache.TileUrl, Constant.FTP_ACHE_IMG + "/" + strGameField[(int)eGameField.eGameId] + "/" + ache.Id.ToString() + ".jpg");
                    consoleWriteLine("--" + ache.Id.ToString()+ " saved");
                }
            }
        }

        /// <summary>
        /// Apply to a game with a specific msid
        /// </summary>
        /// <param name="msid"></param>
        /// <param name="gamertag"></param>
        public void apply(string msid, string gamertag)
        {
            consoleWriteLine("Update game : " + msid + " with " + gamertag);

            int iGameID = DBInterface.getGameExists(msid);
            if (iGameID == -1)
            {
                consoleWriteLine("The game doesn't exists");
                return;
            }

            //get the achievements page
            browser.getWebPageAchievement("about:blank");
            string url = "https://live.xbox.com/fr-FR/Activity/Details?titleId=" + msid + "&compareTo=" + gamertag.Replace(" ", "%20");
            string html = browser.getWebPageAchievement(url);

            //extract achievements data
            JSONData achievements = Extract.extractAchievements(html);
            if (achievements == null)
            {
                consoleWriteLine("No achievement");
                return;
            }

            //create directory
            if (!DBInterface.ftpCreateDirectory(Constant.FTP_ACHE_IMG + "/" + iGameID))
                return;

            consoleWriteLine("Directory created");

            //go through each achievements
            foreach (JSONAchievement ache in achievements._achievements)
            {
                //upload the picture to the ftp
                if (ache.IsHidden)
                    DBInterface.getImg(main.UrlHiddenAchievement, Constant.FTP_ACHE_IMG + "/" + iGameID + "/" + ache.Id.ToString() + ".jpg");
                else
                    DBInterface.getImg(ache.TileUrl, Constant.FTP_ACHE_IMG + "/" + iGameID + "/" + ache.Id.ToString() + ".jpg");

                consoleWriteLine("--Picture : " + ache.Id.ToString() + " saved");
            }
            
        }
    }
}
